<?php
class model_meterials extends database{
	protected $_table     = "nhomnguyenlieu";
	protected $_meterials = "nguyenlieu";
	protected $_provider   = "nhacungcap";
	// đây là hàm thêm nhóm sản phẩm
	public function add_group($data){
		$table = $this->_table;
		$col   = implode(",",array_keys($data));
		$arr   = array_values($data);
		foreach($arr as $arr2){
			$arr3[] = "'$arr2'";
		}
		$val = implode(",",$arr3); 
		$sql = "insert into $table($col) values($val)";
		$this->query($sql);
	}
	//kiểm tra tên nhóm đã tồn tại hay chưa
	public function check_namegroup($name){
		$sql = "select tennhomnguyenlieu from $this->_table where tennhomnguyenlieu ='$name' ";

		$this->query($sql);
		
		if($this->num_rows() == 0){
			return TRUE;
		}else{
			return FALSE;	
		}
	}
	//lấy ra danh sách nhóm nguyên liệu
	public function list_group(){
		$sql = "select * from $this->_table";
		
		$this->query($sql);

		return $this->fetchAll();
	}
	// lây ra danh sách nhóm 
	public function list_groups($start,$limit){
		$sql = "select * from $this->_table order by manhomnguyenlieu DESC limit $start,$limit";
		$this->query($sql);
		return $this->fetchAll();
	}
	//lấy ra danh sách nhà cung cấp
	public function list_provider(){
		$sql = "select * from $this->_provider";
		
		$this->query($sql);

		return $this->fetchAll();
	}
	//thêm mới sản phẩm
	public function add_meterials($data){
		$table = $this->_meterials;
		$col   = implode(",",array_keys($data));
		$arr   = array_values($data);
		foreach($arr as $arr2){
			$arr3[] = "'$arr2'";
		}
		$val = implode(",",$arr3); 
		$sql = "insert into $table($col) values($val)";
		$this->query($sql);
	}
	public function total_meterials(){
		$sql  = "select * from $this->_meterials";

		$this->query($sql);

		return $this->num_rows();
	}
	public function list_meterials($start,$limit){
		$sql = "select * from $this->_meterials inner join nhacungcap on nhacungcap.manhacungcap = nguyenlieu.manhacungcap order by manguyenlieu ASC limit $start,$limit";
		
		$this->query($sql);
		
		return $this->fetchAll();
	}
	public function list_group_meterials($start,$limit,$id){
		$sql = "select * from $this->_meterials inner join nhacungcap on nhacungcap.manhacungcap = nguyenlieu.manhacungcap where manhomnguyenlieu = '$id' order by manguyenlieu ASC limit $start,$limit";
		
		$this->query($sql);
		
		return $this->fetchAll();
	}
	public function total_meterials_group($id){
		$sql  = "select * from $this->_meterials where manhomnguyenlieu = '$id'";

		$this->query($sql);

		return $this->num_rows();
	}
	//xóa sản phẩm 
	public function del_meterials($table,$id){
		$sql = "delete from $table where manguyenlieu = $id";
		
		$this->query($sql);
	}
	//lấy ra thông tin một sản phẩm
	public function getdata($id){
		$sql = "select * from $this->_meterials where manguyenlieu = '$id'";
		
		$this->query($sql);
		
		return $this->fetch();
	}
	public function update_meterials($data,$id){
			foreach($data as $k => $v){
				$arr[] = "$k = '$v'";
			}
			$col = implode(",",$arr);
			$sql = "update $this->_meterials set $col where manguyenlieu = '$id'";
			$this->query($sql);
	}
	//Search
	public function list_search($key,$start,$limit){
		$sql = "select * from $this->_meterials inner join nhacungcap on nhacungcap.manhacungcap = nguyenlieu.manhacungcap where tennguyenlieu like '%$key%' limit $start,$limit";
		$this->query($sql);
		return $this->fetchAll();
	}
	public function total_search($key){
		$sql = "select * from $this->_meterials where tennguyenlieu like '%$key%'";
		$this->query($sql);
		return $this->num_rows();
	}
	public function total_group(){
		$sql  = "select * from $this->_table";
		$this->query($sql);
		return $this->num_rows();
	}
	public function get_group($id){
		$sql = "select * from $this->_table where manhomnguyenlieu = '$id'";
		$this->query($sql);
		return $this->fetch();
	}
	public function update_group($data,$id){
		foreach($data as $k => $v){
			$arr[] = "$k = '$v'";
		}
		$col = implode(",",$arr);
		$sql = "update $this->_table set $col where manhomnguyenlieu = '$id'";
		$this->query($sql);
	}
	public function del_group($table,$id){
		$sql = "delete from $table where manhomnguyenlieu = $id";
		
		$this->query($sql);
	}
}